﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThanNhanTao.Server.Entities;

namespace ThanNhanTao.Server
{
   
   public class PhieuChayThanBll
    {
       bvdn_thannhantaoEntities benhNhanContext;
       public PhieuChayThanBll()
       {
           benhNhanContext = ThanNhanTaoGlobalServer.bvdn_thannhantaoEntities;
       }
       public List<Dm_YeuCau> GetListYeuCau()
       {
           List<Dm_YeuCau> t = (from c in benhNhanContext.Dm_YeuCau where c.KichHoat == true && c.LaChayThan == true select c).ToList();//.OrderByDescending(c => c.LaChayThan).ToList();
           return t;
       }
       public int GetLanChay(int idBenhNhan)
       {
           try
           {
               int maxID = benhNhanContext.ChayThanNhanTaos.Where(c => c.IdBenhNhan == idBenhNhan).Max(u => u.SoLan);
               return maxID;
           }
           catch
           {
               return 0;
           }
       }
       public int GetMaxIDChayThan()
       {
           try
           {
               int maxID = benhNhanContext.ChayThanNhanTaos.Max(u => u.IdChayThanNhanTao);
               return maxID;
           }
           catch
           {
               return 0;
           }
       }
       public int GetMaxIDChiDinh()
       {
           try
           {
               int maxID = benhNhanContext.ChiDinhs.Max(u => u.IdChiDinh);
               return maxID;
           }
           catch
           {
               return 0;
           }
       }

       private int AddChiDinh(ChiDinh chiDinh)
       {
          benhNhanContext.AddToChiDinhs(chiDinh);
          benhNhanContext.SaveChanges();
          return chiDinh.IdChiDinh;
       }
       private int UpdateChiDinh(ChiDinh chiDinh)
       {
           ChiDinh chiDinhTemple = (from c in benhNhanContext.ChiDinhs where c.IdChiDinh == chiDinh.IdChiDinh select c).SingleOrDefault();
           chiDinhTemple.NgayYeuCau = chiDinh.NgayYeuCau;
           benhNhanContext.SaveChanges();
           return chiDinh.IdChiDinh;
       }
       private bool DeleteChiDinh(ChiDinh chiDinh)
       {
           try
           {
               ChiDinh chiDinhTemple = (from c in benhNhanContext.ChiDinhs where c.IdChiDinh == chiDinh.IdChiDinh select c).SingleOrDefault();
               benhNhanContext.DeleteObject(chiDinhTemple);
               benhNhanContext.SaveChanges();
               return true;
           }
           catch
           {
               return false;
           }

       }
       private int AddChayThanNhanTao(ChayThanNhanTao chayThan)
       {
           benhNhanContext.AddToChayThanNhanTaos(chayThan);
           benhNhanContext.SaveChanges();
           return chayThan.IdChayThanNhanTao;
       }
       private int UpdateChayThanNhanTao(ChayThanNhanTao chayThan)
       {
           ChayThanNhanTao chayThanNhanTaoTemple = (from c in benhNhanContext.ChayThanNhanTaos where c.IdChayThanNhanTao == chayThan.IdChayThanNhanTao select c).SingleOrDefault();
           chayThanNhanTaoTemple.NgayChay = chayThan.NgayChay;
           benhNhanContext.SaveChanges();
           return chayThan.IdChayThanNhanTao;
       }
       private bool DeleteChayThanNhanTao(ChayThanNhanTao chayThan)
       {
           try
           {
               ChayThanNhanTao chayThanNhanTaoTemple = (from c in benhNhanContext.ChayThanNhanTaos where c.IdChayThanNhanTao == chayThan.IdChayThanNhanTao select c).SingleOrDefault();
               benhNhanContext.DeleteObject(chayThanNhanTaoTemple);
               benhNhanContext.SaveChanges();
               return true;
           }
           catch
           {
               return false;
           }

       }

       public int AddPhieuChayThan(ChayThanNhanTao chayThan,ChiDinh chiDinh)
       {
           chayThan.IdChayThanNhanTao = GetMaxIDChayThan() + 1;
           int idChayThan=AddChayThanNhanTao(chayThan);
           if (idChayThan > 0)
           {
               chiDinh.IdChiDinh = GetMaxIDChiDinh() + 1;
               chiDinh.IdChayThanNhanTao = idChayThan;
               return AddChiDinh(chiDinh);
           }
           return -1;
       }

       public int UpdatePhieuChayThan(ChayThanNhanTao chayThan, ChiDinh chiDinh)
       {
           int idChayThan = UpdateChayThanNhanTao(chayThan);
           if (idChayThan > 0)
           {
               return UpdateChiDinh(chiDinh);
           }
           return -1;
       }

       public bool DeletePhieuChayThan(ChayThanNhanTao chayThan, ChiDinh chiDinh)
       {
           if (DeleteChiDinh(chiDinh))
           {
               return DeleteChayThanNhanTao(chayThan);
           }
           return false;
       }

       private ChiDinh GetChiDinh(int IdChayThanNhanTao,int idBenhNhan)
       {
           ChiDinh t = (from c in benhNhanContext.ChiDinhs where 
                        c.IdChayThanNhanTao==IdChayThanNhanTao 
                        && c.IdBenhNhan==idBenhNhan select c).FirstOrDefault();
           return t;
       }

       private List<ChayThanNhanTao> GetListChayThanNhanTao(int idBenhNhan)
       {
           List<ChayThanNhanTao> t;
           t = (from c in benhNhanContext.ChayThanNhanTaos.Include("BenhNhan") where c.IdBenhNhan==idBenhNhan select c).ToList();
           return t;
       }

       public List<PhieuChayThanDto> GetListPhieuChayThan(int idBenhNhan, out int lanChayThanNhoNhat)
       {
           List<PhieuChayThanDto> listPhieu = new List<PhieuChayThanDto>();
           List<ChayThanNhanTao> listChayThan = GetListChayThanNhanTao(idBenhNhan);
           lanChayThanNhoNhat = listChayThan.Min(u=>u.SoLan);

           foreach (ChayThanNhanTao ct in listChayThan)
           {
               PhieuChayThanDto dto = new PhieuChayThanDto
                                        {
                                            LanChay = ct.SoLan,
                                            NgayChay = ct.NgayChay,
                                            ChanDoan = ct.BenhNhan.Dm_ChanDoan.TenChanDoan,
                                            obChayThanNhanTao = ct,
                                            obChiDinh = GetChiDinh(ct.IdChayThanNhanTao, idBenhNhan),
                                            delete = false
                                        };
               dto.LaLanChayThanNhoNhat = (lanChayThanNhoNhat == ct.SoLan) ? true : false;
               listPhieu.Add(dto);
           }
           return listPhieu;
       }

       public int TongSoLanChayThanBNDangDieuTri(int? idBenhNhan, DateTime tuNgay, DateTime denNgay, DateTime ngayLamViec)
       {
           return (from a in benhNhanContext.ChayThanNhanTaos
                   where (idBenhNhan != null ? a.BenhNhan.IdBenhNhan == idBenhNhan.Value : true)
                           && a.NgayChay >= tuNgay
                           && a.NgayChay <= denNgay
                           && (a.BenhNhan.NgayRaVien == null || (a.BenhNhan.NgayRaVien.Value > ngayLamViec && a.BenhNhan.NgayVaoVien.Value <= ngayLamViec))
                   select a).Count();
       }

       public int TongSoLanChayThanBNDaXuatVien(int? idBenhNhan, DateTime tuNgay, DateTime denNgay, DateTime ngayLamViec)
       {
           return (from a in benhNhanContext.ChayThanNhanTaos
                   where (idBenhNhan != null ? a.BenhNhan.IdBenhNhan == idBenhNhan.Value : true)
                           && a.NgayChay >= tuNgay
                           && a.NgayChay <= denNgay
                           && (a.BenhNhan.NgayRaVien != null && a.BenhNhan.NgayRaVien.Value <= ngayLamViec)
                   select a).Count();
       }
       public List<BenhNhanEntities> GetDanhSachBenhNhanChayThanTheoNgay(DateTime ngayLamViec, DateTime denNgay, string khuVuc, string maYeuCau, int viTri
                                                                            , string rightClick2, string rightClick3, string rightClick4, string rightClick5
                                                                            , int loaiBenhNhan, int thongKe)
       {
           List<BenhNhanEntities> t;
           DateTime ngayChayTempleteBD = Convert.ToDateTime(ngayLamViec.ToShortDateString() + " 00:00:00");
           DateTime ngayChayTempleteKT = Convert.ToDateTime(denNgay.ToShortDateString() + " 23:59:59");
           //Trường hợp thống kê bệnh nhân chạy thận
           if (thongKe == 0)
           {
               t = (from d in benhNhanContext.ChiDinhs.Include("ChayThanNhanTaos").Include("BenhNhan")
                    orderby d.BenhNhan.Ten
                    where d.ChayThanNhanTao.NgayChay >= ngayChayTempleteBD
                            && d.ChayThanNhanTao.NgayChay <= ngayChayTempleteKT
                            && (!khuVuc.Contains("T") ? d.BenhNhan.MLPrismaflex.Equals(khuVuc) : true)
                            && (viTri != 1 && viTri != 2 ? (maYeuCau.Length > 0 ? d.MaYeuCau == maYeuCau : true) : true)
                            && (viTri == 1 ? (d.MaYeuCau == rightClick2 || d.MaYeuCau == rightClick3) : true)
                            && (viTri == 2 ? (d.MaYeuCau == rightClick4 || d.MaYeuCau == rightClick5) : true)
                            && (loaiBenhNhan > 0 ? d.BenhNhan.LoaiBenhNhan == loaiBenhNhan : true)
                    select new BenhNhanEntities
                    {
                        SoNhapVien = d.BenhNhan.SoNhapVien,
                        Ho = d.BenhNhan.Ho,
                        Ten = d.BenhNhan.Ten,
                        GioiTinh = d.BenhNhan.GioiTinh,
                        Tuoi = d.BenhNhan.Tuoi,
                        SoBHYT = d.BenhNhan.SoBHYT,
                        NgayChay = d.ChayThanNhanTao.NgayChay,
                        NgayVaoVien = d.BenhNhan.NgayVaoVien,
                        NgayRaVien = d.BenhNhan.NgayRaVien,
                        Khu = d.BenhNhan.MLPrismaflex
                    }
                   ).ToList();
           }
           else //Trường hợp thống kê bệnh nhân không chạy thận
           {
               List<int> listIdBenhNhan = (from e in benhNhanContext.ChiDinhs.Include("ChayThanNhanTao")
                                           where e.ChayThanNhanTao.NgayChay >= ngayChayTempleteBD
                                                   && e.ChayThanNhanTao.NgayChay <= ngayChayTempleteKT
                                                   && (viTri != 1 && viTri != 2 ? (maYeuCau.Length > 0 ? e.MaYeuCau == maYeuCau : true) : true)
                                                   && (viTri == 1 ? (e.MaYeuCau == rightClick2 || e.MaYeuCau == rightClick3) : true)
                                                   && (viTri == 2 ? (e.MaYeuCau == rightClick4 || e.MaYeuCau == rightClick5) : true)
                                           select e.IdBenhNhan).Distinct().ToList();
               t = (from c in benhNhanContext.BenhNhans
                    orderby c.Ten
                    where (!listIdBenhNhan.Contains(c.IdBenhNhan))
                        //(
                        //    from d in benhNhanContext.ChayThanNhanTaos
                        //    join e in benhNhanContext.ChiDinhs on d.IdChayThanNhanTao equals e.IdChayThanNhanTao
                        //    where c.IdBenhNhan == d.IdBenhNhan
                        //            && d.NgayChay >= ngayChayTempleteBD
                        //            && d.NgayChay <= ngayChayTempleteKT
                        //            && (viTri != 1 && viTri != 2 ? (maYeuCau.Length > 0 ? e.MaYeuCau == maYeuCau : true) : true)
                        //            && (viTri == 1 ? (e.MaYeuCau == rightClick2 || e.MaYeuCau == rightClick3) : true)
                        //            && (viTri == 2 ? (e.MaYeuCau == rightClick4 || e.MaYeuCau == rightClick5) : true)
                        //    select d
                        //).Count() == 0
                            && (!khuVuc.Contains("T") ? c.MLPrismaflex.Equals(khuVuc) : true)
                            && (loaiBenhNhan > 0 ? c.LoaiBenhNhan == loaiBenhNhan : true)
                    select new BenhNhanEntities
                    {
                        SoNhapVien = c.SoNhapVien,
                        Ho = c.Ho,
                        Ten = c.Ten,
                        GioiTinh = c.GioiTinh,
                        Tuoi = c.Tuoi,
                        SoBHYT = c.SoBHYT,
                        NgayVaoVien = c.NgayVaoVien,
                        NgayRaVien = c.NgayRaVien,
                        Khu = c.MLPrismaflex
                    }
                   ).ToList();
           }
           return t;
       }
    }

   public class PhieuChayThanDto
   {
       public DateTime NgayChay {get; set;}
       public int LanChay { get; set; }
       public ChiDinh obChiDinh { get; set; }
       public ChayThanNhanTao obChayThanNhanTao { get; set; }
       public string hoBN { get; set; }
       public string tuoi { get; set; }
       public string NgayIn { get; set;}
       public bool delete { get; set; }
       public bool Edit { get; set; }
       public bool PrintPhieu { get; set; }
       public string ChanDoan { get; set; }
       public bool LaLanChayThanNhoNhat { get; set; }
   }
}
